import { fileURLToPath } from 'url';
import path from 'path';
import fs from 'fs';
import yaml from 'yaml';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const CONFIG_PATH = path.resolve(__dirname, '../config/imag.yaml');

function loadConfig() {
    if (fs.existsSync(CONFIG_PATH)) {
        const content = fs.readFileSync(CONFIG_PATH, 'utf8');
        return yaml.parse(content);
    } else {
        const defaultConfig = { link_domain: '' };
        saveConfig(defaultConfig);
        return defaultConfig;
    }
}

function saveConfig(config) {
    const yamlContent = yaml.stringify(config);
    fs.mkdirSync(path.dirname(CONFIG_PATH), { recursive: true }); 
    fs.writeFileSync(CONFIG_PATH, yamlContent, 'utf8');
}

export { loadConfig, saveConfig };